package com.sxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxy.controller.common.R;
import com.sxy.dto.SetmealDto;
import com.sxy.pojo.Category;
import com.sxy.pojo.Setmeal;
import com.sxy.service.CategoryService;
import com.sxy.service.SetmealDishService;
import com.sxy.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache", allEntries = true)//表示清理setmealCache下所有缓存数据
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("save....");
        setmealService.saveWithDish(setmealDto);
        return R.success("添加套餐成功~");
    }

    @GetMapping("/page")
    public R<Page> getByPage(int page, int pageSize, String name) {
        log.info("套餐分页查询....");
        //定义page对象
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //创建条件查询
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //添加条件
        lqw.like(name != null, Setmeal::getName, name);
        //添加排序条件
        lqw.orderByDesc(Setmeal::getUpdateTime);
        //执行业务层查询方法
        setmealService.page(setmealPage);
        //拷贝对象
        BeanUtils.copyProperties(setmealPage, setmealDtoPage);

        //获取page中的值
        List<Setmeal> records = setmealPage.getRecords();

        //遍历records集合
        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //将每个item数据拷贝到setmealDto对象
            BeanUtils.copyProperties(item, setmealDto);

            //获取item中的分类id字段
            Long categoryId = item.getCategoryId();

            //使用categoryId来查询categoryName属性
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //将查询到的category对象获取name封装到setmealDto中
                setmealDto.setCategoryName(category.getName());
            }
            //返回setmealDto对象
            return setmealDto;

        }).collect(Collectors.toList());

        //将重新遍历封装的数据添加到setmealDtoPage的records属性中
        setmealDtoPage.setRecords(list);

        return R.success(setmealDtoPage);
    }

    /**
     * 根据id来删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)//表示清理setmealCache下所有缓存数据
    public R<String> deleteByIds(@RequestParam List<Long> ids) {
        log.info("根据ids来删除套餐");
        setmealService.removeWithDish(ids);
        return R.success("删除菜品成功");

    }

    /**
     * 启售或停售菜品
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable int status, Long[] ids) {
        log.info("启用或停用菜品....");
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        for (Long id : ids) {
            setmeal.setId(id);
            setmealService.updateById(setmeal);
        }
        return R.success("操作套餐成功~");
    }

    /**
     * 通过id查询对应的套餐信息并回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id) {
        log.info("通过id查询对应的套餐...");
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 修改菜品
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        log.info("修改菜品信息...");
        setmealService.updateWithDish(setmealDto);
        return R.success("修改菜品成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> getList(Setmeal setmeal) {
        log.info("getList....");
        //创建条件查询器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //添加条件
        lqw.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(Setmeal::getStatus,setmeal.getStatus());
        //添加排序条件
        lqw.orderByDesc(Setmeal::getUpdateTime);
        //执行业务层方法
        List<Setmeal> setmeals = setmealService.list(lqw);

        return R.success(setmeals);
    }
}
